import { useWebComponentStore } from '@/store//web/component'
import { useWebSiteStore } from '@/store//web/site'
import { useWebPageStore } from '@/store/web/page'
import { useWebControlsStore } from '@/store/web/controls'
import { guid } from '@/utils/'

function addComponentData(componentData: IComponent) {
  const useWebPage = useWebPageStore()
  const useWebComponent = useWebComponentStore()
  useWebPage.addComponent_(componentData)
  useWebComponent.setActiveComponentId_(componentData.id)
}
//递归处理在拷贝组件的时候将组件的微博事件也拷贝出来并赋予新的id与拷贝出来的id关联上
function copyGroupComponentControls(componentData: IComponent) {
  let compId: string = ''
  let copyId: string = ''
  const useWebSite = useWebSiteStore()
  const useWebControls = useWebControlsStore()
  _.forEach(componentData.components, (componentData_: IComponent) => {
    copyId = componentData_.id
    compId = guid()
    componentData_.id = compId
    useWebSite.componentMap[componentData.id] = componentData
    useWebControls.addEventControlToControls({ copyId, compId })
    if(componentData_.components && componentData_.components.length > 0) {
      copyGroupComponentControls(componentData_)
    }
  })
}

//递归处理在删除组件的时候将组件的微博事件也删除掉
function deleteComponentControl(componentData: IComponent) {
  const useWebControls = useWebControlsStore()
  const useWebSite = useWebSiteStore()
  _.forEach(componentData.components, (componentData_: IComponent) => {
    useWebControls.deleteControl(componentData_.id)
    delete useWebSite.componentMap[componentData.id]
    if(componentData_.components && componentData_.components.length > 0) {
      deleteComponentControl(componentData_)
    }
  })
}

export {
  copyGroupComponentControls,
  addComponentData,
  deleteComponentControl
}